package central;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import GUI.Ventana;

import control_monitor.Monitor;


public class pieza_C implements Runnable{

	
	private Monitor monitor; 
	int socketM2;
	int socketM3;
	int socketM4;
	int socketR2;
	int socketR1;
	Ventana gui;
	
	
	public pieza_C(Monitor monit,int sokr1,int sokr2,int sokm2,int sokm3,int sokm4, Ventana _gui){
		monitor=monit;
		socketM2=sokm2;
		socketM3=sokm3;
		socketM4=sokm4;
		socketR2=sokr2;
		socketR1=sokr1;
		gui = _gui;
	}
	
	
	
	public void run(){
		
			//ejecutar el camino C
		
			monitor.ejecutar_transicion(6);
			monitor.ejecutar_transicion(1);
			gui.SetRobot(3, 4);
			gui.setnumI(3, -1);
			
			
			Socket socketA = null;
			DataOutputStream sale_flujoR2 = null;
			try {
				socketA=new Socket("localhost",socketR2);
				sale_flujoR2 = new DataOutputStream(socketA.getOutputStream());
				sale_flujoR2.writeUTF("mover pieza de I3 a m2");
				//sale_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			DataInputStream entra_flujoR2 = null;
			try {
				entra_flujoR2 = new DataInputStream(socketA.getInputStream());
				String mensaje=entra_flujoR2.readUTF();//robot confirma movimiento
				System.out.println(mensaje);
				//entra_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				socketA.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			gui.SetRobot(3, 3);
			monitor.ejecutar_transicion(21);
			gui.setMaquina(2, 1, 3);
			
			
			DataOutputStream sale_flujoM2;
			
			Socket socketB = null;
			try {
				socketB=new Socket("localhost",socketM2);
				sale_flujoM2 = new DataOutputStream(socketB.getOutputStream());
				sale_flujoM2.writeUTF("procesar pieza m2");
				//sale_flujoM2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DataInputStream entra_flujoM2;
			try {
				entra_flujoM2 = new DataInputStream(socketB.getInputStream());
				String mensajemaq2=entra_flujoM2.readUTF();//maquina confirma procesamiento
				System.out.println(mensajemaq2);
				//entra_flujoM2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				socketB.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			monitor.ejecutar_transicion(2);
			gui.SetRobot(3, 4);
			gui.setnumI(3, -1);
			
			Socket socketC = null;
			try {
				socketC = new Socket("localhost",socketR2);
				sale_flujoR2 = new DataOutputStream(socketC.getOutputStream());
				sale_flujoR2.writeUTF("mover pieza de I3 a m4");
				//sale_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			try {
				entra_flujoR2 = new DataInputStream(socketC.getInputStream());
				String mensaje2=entra_flujoR2.readUTF();
				System.out.println(mensaje2);
				//entra_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//robot confirma movimiento
			
			try {
				socketC.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			

			gui.SetRobot(3, 1);
			monitor.ejecutar_transicion(22);
			gui.setMaquina(4, 1, 3);
			
			
			DataOutputStream sale_flujoM4;
			Socket socketD = null;
			try {
				socketD = new Socket("localhost",socketM4);
				sale_flujoM4 = new DataOutputStream(socketD.getOutputStream());
				sale_flujoM4.writeUTF("procesar pieza en m4");
				//sale_flujoM4.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//sale_flujoM4.close();

			
			
			DataInputStream entra_flujoM4;
			try {
				entra_flujoM4 = new DataInputStream(socketD.getInputStream());
				String mensajemaq4=entra_flujoM4.readUTF();//maquina confirma procesamiento
				System.out.println(mensajemaq4);
				//entra_flujoM4.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				socketD.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//entra_flujoM4.close();
			
						
			monitor.ejecutar_transicion(19);
			gui.SetRobot(2, 5);
			gui.setMaquina(2, -1, 3);
			
			Socket socketE = null;
			try {
				socketE = new Socket("localhost",socketR2);
				sale_flujoR2 = new DataOutputStream(socketE.getOutputStream());
				sale_flujoR2.writeUTF("mover pieza de m2 a m3 ");
				//sale_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			try {
				entra_flujoR2 = new DataInputStream(socketE.getInputStream());
				String mensaje3=entra_flujoR2.readUTF();
				System.out.println(mensaje3);
				//entra_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//robot confirma movimiento
			try {
				socketE.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			

			gui.SetRobot(2, 1);
			monitor.ejecutar_transicion(20);
			gui.setMaquina(3, 1, 3);
			
			gui.SetRobot(2, 7);
			gui.setMaquina(4, -1, 3);
			

			gui.SetRobot(2, 1);
			monitor.ejecutar_transicion(3);

			
			Socket socketF = null;
			try {
				socketF = new Socket("localhost",socketR2);
				sale_flujoR2 = new DataOutputStream(socketF.getOutputStream());
				sale_flujoR2.writeUTF("mover pieza de m4 a m3");
				//sale_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			try {
				entra_flujoR2 = new DataInputStream(socketF.getInputStream());
				String mensaje4=entra_flujoR2.readUTF();
				System.out.println(mensaje4);
				//entra_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//robot confirma movimiento
			try {
				socketF.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			
			monitor.ejecutar_transicion(4);
			gui.SetRobot(1, 7);
			gui.setMaquina(3, -1, 3);
			//omito aumentar la pieza porque ya esta incluida la primera vez

			
			
			//---------------------------------------------------------------------
			
			
			DataOutputStream sale_flujoM3;
			Socket socketG = null;
			try {
				socketG = new Socket("localhost",socketM3);
				sale_flujoM3 = new DataOutputStream(socketG.getOutputStream());
				sale_flujoM3.writeUTF("procesar pieza en m3");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//sale_flujoM3.close();
			
			DataInputStream entra_flujoM3;
			try {
				entra_flujoM3 = new DataInputStream(socketG.getInputStream());
				String mensajemaq=entra_flujoM3.readUTF();//maquina confirma procesamiento
				System.out.println(mensajemaq);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				socketG.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//entra_flujoM3.close();
			
			gui.SetRobot(1, 0);
			monitor.ejecutar_transicion(5);
			gui.setnumO(3, 1);
					
			DataOutputStream sale_flujoR1;
			Socket socketH = null;
			try {
				socketH=new Socket("localhost",socketR1);
				sale_flujoR1 = new DataOutputStream(socketH.getOutputStream());
				sale_flujoR1.writeUTF("mover pieza de m4 a O3");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//sale_flujoR1.close();
			
			DataInputStream entra_flujoR1;
			try {
				entra_flujoR1 = new DataInputStream(socketH.getInputStream());
				String mensaje5=entra_flujoR1.readUTF();//robot confirma movimiento
				System.out.println(mensaje5);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				socketH.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//entra_flujoR1.close();

			System.out.println("ya termino el camino C");
	}
}
